# import requests


# url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10"
# res = requests.get(url)
# datas = res.json()
# for data in datas:
#     print(data)
#     data_id = data['id']
#     data_name = data['name']
#     data_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{data_id}.jpg?gameid=g10"
#     data_url_response = requests.get(data_url)
#     # print(data_id,data_name)
#     print(data_url_response.content)
#     # print(data['name'],data['id'])
#     break



import requests
# import pymongo
import os
import shutil

# # 连接到mongo数据库
# client = pymongo.MongoClient('localhost', port=27017)
# db = client.get_database('率土之滨')
# collection = db.get_collection('武将库')



url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10"
res = requests.get(url)
datas = res.json()

# # 插入列表
# collection.insert_many(datas)
# client.close()


# 保存图片
directory_path = "static/率土之滨/武将库"
if not os.path.exists(directory_path):
    os.makedirs(directory_path)
else:
    shutil.rmtree(directory_path)
    os.makedirs(directory_path)



for data in datas:
    # print(data)
    data_id = data['id']
    data_name = data['name']
    data_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{data_id}.jpg?gameid=g10"
    data_url_response = requests.get(data_url)
    # print(data_id,data_name)
    print(data_url_response.content)
    # print(data['name'],data['id'])
    with open(f"{directory_path}/{data_name}.jpg", "wb") as f:
        f.write(data_url_response.content)
    # break



